﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class RC_Catalogs : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private RecruitCatalogBLL catBLL = new RecruitCatalogBLL();
        private IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindEmpType();
                ListYear();
                cmbYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
            }
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = etBLL.ListEmployeeTypes();
            cmbEmployeeType.DataSource = infoList;
            cmbEmployeeType.DataBind();
        }
        public void ListYear()
        {
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = 2500; i <= 2600; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cmbYear.DataSource = dt;
            cmbYear.DataBind();
        }
        private void LoadDataGrid()
        {
            Nullable<int> EmpTypeSeq = string.IsNullOrEmpty(cmbEmployeeType.SelectedValue) ? (int?)null : Int32.Parse(cmbEmployeeType.SelectedValue);
            Nullable<int> bgYear = string.IsNullOrEmpty(cmbYear.SelectedValue) ? (int?)null : Int32.Parse(cmbYear.SelectedValue);

            try
            {
                DataSet ds = catBLL.LoadCatalogs((int?)null, EmpTypeSeq, bgYear, (int?)null);
                DataTable dt = null;

                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];

                    RadGrid2.DataSource = dt;
                    RadGrid2.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeType.SelectedValue;
            Session["BGYEAR"] = cmbYear.SelectedValue;

            if (e.CommandName == "Delete")
            {
                int Sequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cat_seq"]);
                catBLL.Delete(Sequence, ssMng.EmpSeqI);
            }
            else if (e.CommandName == "PrintPreview")
            {
                //DataTable dt = null;
                //int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_seq"]);

                //DataSet ds = BLL.LoadCommands(SystemInfo._CmdEmployee, seq, (int?)null, (int?)null);
                //if (ds.Tables.Count > 0)
                //    dt = ds.Tables[0];

                //Session["dtReport"] = dt;
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "openReport()", true);
                //return;
            }
            LoadDataGrid();
        }
        protected void cmbEmployeeType_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeType.SelectedValue;
            LoadDataGrid();
        }

        protected void cmbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGrid();
        }
    }
}
